﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using RTUParameterConfig.Class;

namespace RTUParameterConfig
{
    public partial class WaterLevelSetting : Form
    {
        public int intIndex;

        public WaterLevelSetting(int index)
        {
            InitializeComponent();
            intIndex = index;
            SetValue();
        }

        /// <summary>
        /// 设置水位计配置
        /// </summary>
        public void SetValue()
        {
            try
            {
                if (intIndex == 0)
                {
                    nud1.Value = Convert.ToDecimal(BitConverter.ToInt32(ParameterConfig.rtuConfigInfo.WaterBaseLevel1.Reverse<byte>().ToArray(), 0) / 1000.0);
                    nud2.Value = Convert.ToDecimal(BitConverter.ToInt32(ParameterConfig.rtuConfigInfo.WaterCorrectionLevel1.Reverse<byte>().ToArray(), 0) / 1000.0);
                    nud3.Value = Convert.ToDecimal(BitConverter.ToUInt32(ParameterConfig.rtuConfigInfo.WaterCorrParame1.Reverse<byte>().ToArray(), 0) / 10000.0);
                    nud4.Value = Convert.ToDecimal(BitConverter.ToUInt32(ParameterConfig.rtuConfigInfo.WaterLevelHAlarm1.Reverse<byte>().ToArray(), 0) / 100.0);
                    nud5.Value = Convert.ToDecimal(BitConverter.ToUInt32(ParameterConfig.rtuConfigInfo.WaterLevelLAlarm1.Reverse<byte>().ToArray(), 0) / 100.0);
                    nud6.Value = Convert.ToDecimal(BitConverter.ToUInt32(ParameterConfig.rtuConfigInfo.WaterLevelAlarmH1.Reverse<byte>().ToArray(), 0) / 100.0);
                    nud7.Value = Convert.ToDecimal(BitConverter.ToUInt32(ParameterConfig.rtuConfigInfo.WaterLevelAlarmL1.Reverse<byte>().ToArray(), 0) / 100.0);
                    nud8.Value = Convert.ToDecimal(ParameterConfig.rtuConfigInfo.WaterLevelChangeAlarm[0] / 100.0);
                    nud9.Value = ParameterConfig.rtuConfigInfo.WaterFilter[0];
                    nud10.Value = Convert.ToDecimal(ParameterConfig.rtuConfigInfo.WaterFilterParam[0] / 10.0);
                }

                if (intIndex == 1)
                {
                    nud1.Value = Convert.ToDecimal(BitConverter.ToInt32(ParameterConfig.rtuConfigInfo.WaterBaseLevel2.Reverse<byte>().ToArray(), 0) / 1000.0);
                    nud2.Value = Convert.ToDecimal(BitConverter.ToInt32(ParameterConfig.rtuConfigInfo.WaterCorrectionLevel2.Reverse<byte>().ToArray(), 0) / 1000.0);
                    nud3.Value = Convert.ToDecimal(BitConverter.ToUInt32(ParameterConfig.rtuConfigInfo.WaterCorrParame2.Reverse<byte>().ToArray(), 0) / 10000.0);
                    nud4.Value = Convert.ToDecimal(BitConverter.ToUInt32(ParameterConfig.rtuConfigInfo.WaterLevelHAlarm2.Reverse<byte>().ToArray(), 0) / 100.0);
                    nud5.Value = Convert.ToDecimal(BitConverter.ToUInt32(ParameterConfig.rtuConfigInfo.WaterLevelLAlarm2.Reverse<byte>().ToArray(), 0) / 100.0);
                    nud6.Value = Convert.ToDecimal(BitConverter.ToUInt32(ParameterConfig.rtuConfigInfo.WaterLevelAlarmH2.Reverse<byte>().ToArray(), 0) / 100.0);
                    nud7.Value = Convert.ToDecimal(BitConverter.ToUInt32(ParameterConfig.rtuConfigInfo.WaterLevelAlarmL2.Reverse<byte>().ToArray(), 0) / 100.0);
                    nud8.Value = Convert.ToDecimal(ParameterConfig.rtuConfigInfo.WaterLevelChangeAlarm[1] / 100.0);
                    nud9.Value = ParameterConfig.rtuConfigInfo.WaterFilter[1];
                    nud10.Value = Convert.ToDecimal(ParameterConfig.rtuConfigInfo.WaterFilterParam[1] / 10.0);
                }
            }
            catch
            {
                Common.ShowErrorMessage("加载高级设置参数失败！");
            }
        }

        /// <summary>
        /// 获取水位计配置
        /// </summary>
        public void GetValue()
        {
            if (intIndex == 0)
            {
                ParameterConfig.rtuConfigInfo.WaterBaseLevel1 = BitConverter.GetBytes(Convert.ToInt32(nud1.Value * 1000)).Reverse<byte>().ToArray();
                ParameterConfig.rtuConfigInfo.WaterCorrectionLevel1 = BitConverter.GetBytes(Convert.ToInt32(nud2.Value * 1000)).Reverse<byte>().ToArray();
                ParameterConfig.rtuConfigInfo.WaterCorrParame1 = BitConverter.GetBytes(Convert.ToUInt32(nud3.Value * 10000)).Reverse<byte>().ToArray();
                ParameterConfig.rtuConfigInfo.WaterLevelHAlarm1 = BitConverter.GetBytes(Convert.ToUInt32(nud4.Value * 100)).Reverse<byte>().ToArray();
                ParameterConfig.rtuConfigInfo.WaterLevelLAlarm1 = BitConverter.GetBytes(Convert.ToUInt32(nud5.Value * 100)).Reverse<byte>().ToArray();
                ParameterConfig.rtuConfigInfo.WaterLevelAlarmH1 = BitConverter.GetBytes(Convert.ToUInt32(nud6.Value * 100)).Reverse<byte>().ToArray();
                ParameterConfig.rtuConfigInfo.WaterLevelAlarmL1 = BitConverter.GetBytes(Convert.ToUInt32(nud7.Value * 100)).Reverse<byte>().ToArray();
                ParameterConfig.rtuConfigInfo.WaterLevelChangeAlarm[0] = Convert.ToByte(nud8.Value * 100);
                ParameterConfig.rtuConfigInfo.WaterFilter[0] = Convert.ToByte(nud9.Value);
                ParameterConfig.rtuConfigInfo.WaterFilterParam[0] = Convert.ToByte(nud10.Value * 10);
            }

            if (intIndex == 1)
            {
                ParameterConfig.rtuConfigInfo.WaterBaseLevel2 = BitConverter.GetBytes(Convert.ToInt32(nud1.Value * 1000)).Reverse<byte>().ToArray();
                ParameterConfig.rtuConfigInfo.WaterCorrectionLevel2 = BitConverter.GetBytes(Convert.ToInt32(nud2.Value * 1000)).Reverse<byte>().ToArray();
                ParameterConfig.rtuConfigInfo.WaterCorrParame2 = BitConverter.GetBytes(Convert.ToUInt32(nud3.Value * 10000)).Reverse<byte>().ToArray();
                ParameterConfig.rtuConfigInfo.WaterLevelHAlarm2 = BitConverter.GetBytes(Convert.ToUInt32(nud4.Value * 100)).Reverse<byte>().ToArray();
                ParameterConfig.rtuConfigInfo.WaterLevelLAlarm2 = BitConverter.GetBytes(Convert.ToUInt32(nud5.Value * 100)).Reverse<byte>().ToArray();
                ParameterConfig.rtuConfigInfo.WaterLevelAlarmH2 = BitConverter.GetBytes(Convert.ToUInt32(nud6.Value * 100)).Reverse<byte>().ToArray();
                ParameterConfig.rtuConfigInfo.WaterLevelAlarmL2 = BitConverter.GetBytes(Convert.ToUInt32(nud7.Value * 100)).Reverse<byte>().ToArray();
                ParameterConfig.rtuConfigInfo.WaterLevelChangeAlarm[1] = Convert.ToByte(nud8.Value * 100);
                ParameterConfig.rtuConfigInfo.WaterFilter[1] = Convert.ToByte(nud9.Value);
                ParameterConfig.rtuConfigInfo.WaterFilterParam[1] = Convert.ToByte(nud10.Value * 10);
            }
        }

        /// <summary>
        /// 关闭前保存修改的配置信息
        /// </summary>
        private void WaterLevelSetting_FormClosing(object sender, FormClosingEventArgs e)
        {
            GetValue();
            this.Dispose();
        }
    }
}
